<template>
  <div class="detailsItem">
    <div class="dataShow">
      <div class="dataTitle">
        <span>{{title}}</span>
      </div>
      <div v-if="infoArray" class="dataBody">
        <div class="dataBodyItem" v-for="item in infoArray" :key="item._key || item.key" >
          <div class="label">{{ item.label }}:</div>
          <div v-if="item.edit && edit" class="value-wrap">
            <el-input class="ml-10" v-model="editValue"></el-input>
            <el-button
              icon="el-icon-close"
              type="danger"
              class="ml-10"
              circle
              @click="edit = false"
            ></el-button>
            <el-button
              icon="el-icon-check"
              type="success"
              circle
              @click="confirm(item.edit)"
            ></el-button>
          </div>
          <div v-else class="value-wrap">
            <div class="value">{{ dataItems[item.key] || '-' }}</div>
            <el-button
              v-if="item.edit"
              icon="el-icon-edit"
              type="primary"
              class="ml-10"
              circle
              @click="editShow(item)"
            ></el-button>
          </div>
        </div>
      </div>
      <slot v-else></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'detailsItem',
  props: {
    dataItems: Object,
    infoArray: Array,
    title: String
  },
  data () {
    return {
      showHeader: false,
      edit: false,
      editValue: ''
    }
  },
  methods: {
    editShow (item) {
      this.edit = true
      this.editValue = this.dataItems[item.key]
    },
    confirm (type) {
      if (!this.editValue) return this.$message.warning('编辑内容不可为空！')
      this.$emit('edit', type, this.editValue, () => {
        this.edit = false
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .detailsItem {
    margin: 20px 10px;
    border-bottom: 1px solid #ededed;

    .dataTitle {
      text-align: left;

      span {
        font-size: 20px;
        font-weight: bold;
      }
    }

    .dataBody {
      display: flex;
      flex-wrap: wrap;
      margin: 20px 10px;

      .dataBodyItem {
        display: flex;
        flex-wrap: nowrap;
        width: 30%;
        margin: 4px 10px;
        font-size: 14px;
        line-height: 32px;
        color: #333;

        .label {
          min-width: 150px;
          text-align: right;
        }

        .value {
          padding-left: 10px;
          border: none;
          outline: none;
        }
      }

      .value-wrap {
        display: flex;
        // flex-wrap: auto;
      }

      .icon {
        margin-left: 10px;

        &:hover {
          cursor: pointer;
        }
      }
    }
  }
</style>
